//
//  XLTextView.m
//  Current
//
//  Created by 于鹏 on 2017/8/23.
//  Copyright © 2017年 YIVIEW. All rights reserved.
//

#import "XLTextView.h"
//#import <BaseModule/NSString+XLSize.h>
#import "NSString+XLSize.h"
#define kTextViewWidth self.frame.size.width
#define kTextViewHeight self.frame.size.height
@interface XLTextView()

@property (nonatomic, weak) UILabel *placeholderLabel;

@end

@implementation XLTextView

#pragma mark - LifeCycle
- (instancetype)init {
    self = [super init];
    if (self) {
        [self configUI];
        [self addObserver];
    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self configUI];
        [self addObserver];    
    }
    return self;
}

- (void)configUI {
    [self addSubview:self.placeholderLabel];
//    [self addSubview:self.iconButton];
    
//    self.backgroundColor = [UIColor whiteColor];
    self.backgroundColor = [UIColor clearColor];
    self.font = [UIFont systemFontOfSize:15.];//设置默认字体
    //相关属性设置
    [self setReturnKeyType:UIReturnKeyDefault];//返回键的类型
    [self setKeyboardType:UIKeyboardTypeDefault];//键盘类型
    self.scrollEnabled = YES;//是否可以拖动
    self.autoresizingMask = UIViewAutoresizingFlexibleHeight;//自适应高度
    self.delegate = self;
}

-(void)addObserver{
     [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self];//监听文字的改变
}
- (void)textDidChange{
    self.placeholderLabel.hidden = self.hasText;//hasText 是一个系统的BOOL属性,如果UITextView输入了文字hasText就是YES,反之就是NO
    
    
    
}
- (void)layoutSubviews{
    [super layoutSubviews];
//    self.placeholderLabel.frame = CGRectMake(8, 0, kTextViewWidth, kTextViewHeight);
    self.placeholderLabel.frame = CGRectMake(8, 8, kTextViewWidth-8, 20);

//    CGSize iconSize = CGSizeMake(25, 25);
//    self.iconButton.frame = CGRectMake(kTextViewWidth-10-iconSize.width, (kTextViewHeight-iconSize.height)/2, iconSize.width, iconSize.height);
//    if (self.contentSize.width > kTextViewWidth-iconSize.width-10) {
//        self.contentInset = UIEdgeInsetsMake(0, 0, 0, iconSize.width+10);
//    } else {
//        self.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
//
//    }
}

#pragma mark - Setter
- (void)setPlaceholder:(NSString *)placeholder {
    _placeholder = placeholder;
    //设置文字
    self.placeholderLabel.text = placeholder;
    //重新计算子控件frame
    [self setNeedsLayout];
}

- (void)setPlaceholderColor:(UIColor *)placeholderColor {
    _placeholderColor = placeholderColor;
    //设置颜色
    self.placeholderLabel.textColor = placeholderColor;
}


#pragma mark  重写这个set方法保持font一致
- (void)setFont:(UIFont *)font{
    [super setFont:font];
    self.placeholderLabel.font = font;
    //重新计算子控件frame
    [self setNeedsLayout];
}

#pragma mark 重写- (void)setText:(NSString *)text 和 - (void)setAttributedText:(NSAttributedString *)attributedText 来控制UILabel的显示和隐藏
- (void)setText:(NSString *)text{
    [super setText:text];
    [self textDidChange];//调用通知回调
}

- (void)setAttributedText:(NSAttributedString *)attributedText{
    
    [super setAttributedText:attributedText];
    [self textDidChange];//调用通知回调
    
}

#pragma mark - Getter
- (UILabel *)placeholderLabel {
    if (!_placeholderLabel) {
        UILabel *placeholderLabel = [[UILabel alloc]init];//添加一个占位label
        placeholderLabel.backgroundColor = [UIColor clearColor];
        placeholderLabel.numberOfLines = 0;
        placeholderLabel.font = [UIFont systemFontOfSize:16];
        [self addSubview:placeholderLabel];
        _placeholderLabel = placeholderLabel;//赋值保存
        self.placeholderColor = [UIColor lightGrayColor];//设置占位文字默认颜色
    }
    return _placeholderLabel;
}

#pragma mark - 销毁通知
- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:UITextViewTextDidChangeNotification];
}

#pragma mark - UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView {
    if (self.xl_textViewDelegate && [self.xl_textViewDelegate respondsToSelector:@selector(xl_textView:updatedText:)]) {
        [self.xl_textViewDelegate xl_textView:self updatedText:textView.text];
    }
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
//    NSString *string = [textView.text stringByReplacingCharactersInRange:range withString:text ];
   
    return YES;
}
#pragma mark - Public Method
- (CGFloat)xl_textViewHeight {
    CGFloat height = [self.text xl_heightWithFont:self.font constrainedToWidth:kTextViewWidth];
    return height;
}
@end
